Imports System
Imports NUnit.Framework
Imports ValidationFramework

Namespace BusinessBaseClasses
    Public Class ValidatableBaseExample
#Region "Example"
        Public Class Person
            Inherits ValidatableBase

            ' Fields
            Private _emailAddress As String
            Public Const EmailAddressMember As String = "EmailAddress"
            Private _firstName As String
            Public Const FirstNameMember As String = "FirstName"
            Private _lastName As String
            Public Const LastNameMember As String = "LastName"


            ' Methods
            Public Sub New()
                MyBase.New()
                PropertyValidationManager.ValidateAll()
            End Sub


            ' Properties
            <RegexRule("\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*", _
                       ErrorMessage:="Invalid email format."), _
                RequiredStringRule(), _
                LengthStringRule(50)> _
            Public Property EmailAddress() As String
                Get
                    Return _emailAddress
                End Get
                Set(ByVal value As String)
                    _emailAddress = value
                End Set
            End Property

            <LengthStringRule(4), _
                RequiredStringRule()> _
            Public Property FirstName() As String
                Get
                    Return _firstName
                End Get
                Set(ByVal value As String)
                    _firstName = value
                End Set
            End Property

            <LengthStringRule(50, Minimum:=2), _
                RequiredStringRule()> _
            Public Property LastName() As String
                Get
                    Return _lastName
                End Get
                Set(ByVal value As String)
                    _lastName = value
                End Set
            End Property


        End Class


        <TestFixture()> _
        Public Class ExampleUsage
            ' Methods
            <Test()> _
            Public Sub RunIsValid()
                Dim _person As New Person

                ' Initially not valid
                Assert.IsFalse(_person.IsValid)

                ' Set FirstName, LastName and EmailAddress
                _person.FirstName = "John"
                _person.LastName = "Smith"
                _person.EmailAddress = "John.Smith@email.com"

                ' It is now valid
                Assert.IsTrue(_person.IsValid)
            End Sub

            <Test()> _
            Public Sub RunValidatorResultsInError()
                Dim _person As New Person
                ' Initially there should be 3 items in ValidatorResultsInError as only 3 
                ' properties have required rules.
                Assert.AreEqual(3, _person.ValidatorResultsInError.Count)

                ' Set LastName to a value and count goes down to 2
                _person.LastName = "Smith"

                ' Since IsValid has not been called the count will not have changed
                Assert.AreEqual(3, _person.ValidatorResultsInError.Count)
                Assert.IsFalse(_person.IsValid)
                ' Since IsValid has now been called the count will have changed
                Assert.AreEqual(2, _person.ValidatorResultsInError.Count)
            End Sub

        End Class
#End Region
    End Class
End Namespace